import { _decorator } from "cc";
import { Prefab } from "cc";
import { ScrollView } from "cc";
import { Component } from "cc";
import JScrollExceedHide from "../JScrollExceedHide";
import { instantiate } from "cc";
import JNScrollViewItem from "./JNScrollViewItem";
import { NodeEventType } from "cc";
const { ccclass, property } = _decorator;


//一个通过对象控制的 ScrollView
//预制体必须拥有 JNScrollViewItem<T> 组件
@ccclass('JNScrollView')
export default class JNScrollView extends ScrollView {

    @property(Prefab)
    itemPrefab:Prefab; //预制体

    exceed:JScrollExceedHide;

    events:{[key:string]:Function} = {};

    onLoad(){

        // //清除数据
        // this.content.destroyAllChildren();

        //挂载虚拟列表组件
        if(!this.node.getComponent(JScrollExceedHide)){
            let exceed = this.node.addComponent(JScrollExceedHide);
            exceed.view = this.view;
            exceed.content = this.content;
            this.exceed = exceed;
        }

    }

    //刷新数据
    refreshData<T>(dataList: T[]){

        //清除数据
        this.content.destroyAllChildren();

        //添加新数据
        dataList.forEach(data => {

            let item = instantiate(this.itemPrefab);
            //获取 JNScrollViewItem 组件赋值
            let itemSC = item.getComponent(JNScrollViewItem);
            itemSC.data = data;
            itemSC.onInit(itemSC.data);
            this.content.addChild(item);
            
            //添加事件
            Object.keys(this.events).forEach(type => {
                item.off(type);
                item.on(type,(e) => {
                    this.events[type](item.getSiblingIndex(),e); //传入下标和事件
                });
            })

        })

        // this.unscheduleAllCallbacks();
        // this.scheduleOnce(() => {
        // })

    }

    //添加数据
    addData<T>(data:T){

        let item = instantiate(this.itemPrefab);
        //获取 JNScrollViewItem 组件赋值
        let itemSC = item.getComponent(JNScrollViewItem);
        itemSC.data = data;
        itemSC.onInit(itemSC.data);
        this.content.addChild(item);

        //添加事件
        Object.keys(this.events).forEach(type => {
            item.off(type);
            item.on(type,(e) => {
                this.events[type](item.getSiblingIndex(),e); //传入下标和事件
            });
        })

        //刷新虚拟列表
        this.scheduleOnce(() => 
            this.exceed.onUpdate()
        )

    }

    //删除数据
    delData<T>(data:T){

        let items = this.getItems();

        for (let index = 0; index < items.length; index++) {
            const child = items[index];
            if(child.data == data) {
                //销毁
                child.node.destroy();
            }
        }

        //刷新虚拟列表
        this.scheduleOnce(() => 
            this.exceed.onUpdate()
        )
        
    }

    //获取列表中Item的组件
    getItems<T extends JNScrollViewItem<any>>():T[]{
        return (this.content.getComponentsInChildren(JNScrollViewItem) as T[]).filter(item => item.node.active && item.node.isValid);
    }

    //获取指定数据的组件
    getDataItems<T extends JNScrollViewItem<any>,D>(data:D):T[]{
        return this.getItems<T>().filter(item => item.data == data);
    }

    //获取数据
    getData<T>():T[]{
        return this.getItems().map(item => (item.data as T));
    }

    //向Item 添加监听事件
    addItemEvent(type: string | NodeEventType,callback:Function){
        this.events[type] = callback;
        this.getItems().forEach((item) => {
            item.node.off(type);
            item.node.on(type,(e) => {
                callback(item.node.getSiblingIndex(),e); //传入下标和事件
            });
        })
    }


}

